/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "cpputils/CppUtils.hpp"
#include "cpputils/io/ByteArrayInputOutputStream.hpp"

namespace cpputils {
	namespace io {
		// --------------------------------------------------------------------------------------------------
		ByteArrayInputOutputStream::ByteArrayInputOutputStream(unsigned int nBlockSize) {
			this->m_nBlockSize = nBlockSize;
			this->m_bDumpMode = false;
			clear();
		}

		// --------------------------------------------------------------------------------------------------
		ByteArrayInputOutputStream::~ByteArrayInputOutputStream() {
			m_aBuffers.clear();
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::setDumpMode(bool bValue) {
			this->m_bDumpMode = bValue;
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::write(ByteBuffer* poBuffer) {
			boost::mutex::scoped_lock oScopedLock(m_mxBuffers);
			if ( this->m_itCurrentWriteBuffer == m_aBuffers.end() ) {
				createNewBuffer();
			}
			int nBytesRemaining = poBuffer->getLen();
			do {
				nBytesRemaining -= writeBuffer ( poBuffer, nBytesRemaining );
			}
			while(nBytesRemaining > 0);
			this->m_nAllSize+=poBuffer->getLen();

			if(m_bDumpMode) {
				std::cout << " ***Wrote*** ";
				poBuffer->dump();
			}
		}

		// --------------------------------------------------------------------------------------------------
		unsigned int ByteArrayInputOutputStream::writeBuffer(
				ByteBuffer* poBuffer,
				unsigned int nBytesRemaining) {
			unsigned int nResult = 0;
			if(nBytesRemaining > (*m_itCurrentWriteBuffer)->getCapacity() - m_nCurrentWritePos) {
				(*m_itCurrentWriteBuffer)->append (
						poBuffer->getData() + (poBuffer->getLen() - nBytesRemaining),
						(*m_itCurrentWriteBuffer)->getCapacity() - m_nCurrentWritePos
				);
				nResult = (*m_itCurrentWriteBuffer)->getCapacity() - m_nCurrentWritePos;
			}
			else {
				(*m_itCurrentWriteBuffer)->append(
						poBuffer->getData() + (poBuffer->getLen() - nBytesRemaining),
						nBytesRemaining
				);
				nResult = nBytesRemaining;
			}
			m_nCurrentWritePos+= nResult;
			if(m_nCurrentWritePos >= (*m_itCurrentWriteBuffer)->getCapacity()) {
				switchToNextWriteBuffer();
			}
			return nResult;
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::createNewBuffer() {
			ByteBuffer::SharedPtr poBuffer (new ByteBuffer (this->m_nBlockSize) );
			m_aBuffers.push_back(poBuffer);
			this->m_itCurrentWriteBuffer =  --m_aBuffers.end();
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::switchToNextWriteBuffer() {
			this->m_itCurrentWriteBuffer++;
			if ( this->m_itCurrentWriteBuffer == m_aBuffers.end() ) {
				createNewBuffer();
			}
			m_nCurrentWritePos = 0;
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::switchToNextReadBuffer() {
			(*m_itCurrentReadBuffer)->setLen ( 0 );
			m_aBuffers.push_back ( (*m_itCurrentReadBuffer) );
			this->m_itCurrentReadBuffer++;
			m_aBuffers.pop_front();
			m_nCurrentReadPos = 0;
		}

		// --------------------------------------------------------------------------------------------------
		int ByteArrayInputOutputStream::read(ByteBuffer* poBuffer) {
			boost::mutex::scoped_lock oScopedLock(m_mxBuffers);
			if ( this->m_itCurrentReadBuffer == m_aBuffers.end() ) {
				this->m_itCurrentReadBuffer = m_aBuffers.begin();
			}
			unsigned int nTotalReadedBytes = 0;
			if ( this->m_itCurrentReadBuffer != m_aBuffers.end() ) {
				if ( (*m_itCurrentReadBuffer)->getLen() > 0 ) {
					do {
						unsigned int nReadBytes = poBuffer->getAvailableCapacity();
						if(nReadBytes>(*m_itCurrentReadBuffer)->getLen()-m_nCurrentReadPos) {
							nReadBytes = (*m_itCurrentReadBuffer)->getLen()-m_nCurrentReadPos;
						}
						poBuffer->append (
							(*m_itCurrentReadBuffer)->getData() + m_nCurrentReadPos,
							nReadBytes
						);
						m_nCurrentReadPos += nReadBytes;
						if( m_nCurrentReadPos >= (*m_itCurrentReadBuffer)->getCapacity())
							switchToNextReadBuffer();
						nTotalReadedBytes+=nReadBytes;
					}
					while(poBuffer->getAvailableCapacity() != 0
						&& m_itCurrentReadBuffer != m_aBuffers.end()
						&& m_nCurrentReadPos < (*m_itCurrentReadBuffer)->getLen()
					);
				}
			}

			if(m_bDumpMode) {
				std::cout << " ***Read*** ";
				poBuffer->dump();
			}

			this->m_nAllSize-=nTotalReadedBytes;
			return nTotalReadedBytes;
		}

		// --------------------------------------------------------------------------------------------------
		void ByteArrayInputOutputStream::clear() {
			boost::mutex::scoped_lock oScopedLock(m_mxBuffers);
			m_aBuffers.clear();
			this->m_itCurrentWriteBuffer = m_aBuffers.end();
			this->m_itCurrentReadBuffer = m_aBuffers.end();
			this->m_nCurrentWritePos = 0;
			this->m_nCurrentReadPos = 0;
			this->m_nAllSize = 0;
		}

		// --------------------------------------------------------------------------------------------------
		bool ByteArrayInputOutputStream::empty() {
			return size() == 0;
		}

		// --------------------------------------------------------------------------------------------------
		unsigned int ByteArrayInputOutputStream::size() {
			boost::mutex::scoped_lock oScopedLock(m_mxBuffers);
			return m_nAllSize;
		}
	}
}
